package com.tencent.wxcloudrun.manager;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencent.wxcloudrun.constant.RedisKeyConstant;
import com.tencent.wxcloudrun.domain.*;
import com.tencent.wxcloudrun.dto.ActiQueryPostResDTO;
import com.tencent.wxcloudrun.enums.ActiJoinStatEnum;
import com.tencent.wxcloudrun.service.*;
import com.tencent.wxcloudrun.util.RedisUtil;
import com.tencent.wxcloudrun.vo.request.acti.*;
import com.tencent.wxcloudrun.vo.response.acti.*;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author ZhaoZiYin
 * @Date 2025/4/22
 * @Description 活动查询管理端
 */
@Service
@Slf4j
public class ActiQueryManager {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private TActiService actiService;

    @Resource
    private TActiRewardService actiRewardService;

    @Resource
    private TActiFillService actiFillService;

    @Resource
    private TActiPostService actiPostService;

    @Resource
    private TActiUserImgService actiUserImgService;

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: ActiListResVo
     * @Description: 查询活动列表
     */
    public ActiListResVo list(ActiListReqVo reqVo) {
        ActiListResVo resVo = new ActiListResVo();
        if (Objects.isNull(reqVo.getActiStatus()) && !StringUtils.hasLength(reqVo.getActiTitle())) {
            String actiListStr = redisUtil.get(RedisKeyConstant.ACTI_LIST);
            if (StringUtils.hasLength(actiListStr)) {
                return JSONObject.parseObject(actiListStr, ActiListResVo.class);
            }
        }
        List<TActi> actiList = actiService.listActi(reqVo.getActiStatus(), reqVo.getActiTitle());
        if (!CollectionUtils.isEmpty(actiList)) {
            resVo.build(actiList);
        }
        if (Objects.isNull(reqVo.getActiStatus()) && !StringUtils.hasLength(reqVo.getActiTitle())) {
            redisUtil.set(RedisKeyConstant.ACTI_LIST, JSONObject.toJSONString(resVo));
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: ActiQueryResVo
     * @Description: 查询活动详情
     */
    public ActiQueryResVo query(ActiQueryReqVo reqVo) {
        ActiQueryResVo resVo = new ActiQueryResVo();
        String key = RedisKeyConstant.getActiInfoRedisKey(reqVo.getActiId());
        String actiStr = redisUtil.get(key);
        if (StringUtils.hasLength(actiStr)) {
            return JSONObject.parseObject(actiStr, ActiQueryResVo.class);
        }
        TActi acti = actiService.getById(reqVo.getActiId());
        List<TActiReward> actiRewardList = actiRewardService.listByActiId(reqVo.getActiId());
        List<TActiFill> actiFillList = actiFillService.listByActiId(reqVo.getActiId());
        if (Objects.nonNull(acti)) {
            resVo.build(acti, actiRewardList, actiFillList);
            redisUtil.set(key, JSONObject.toJSONString(resVo));
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: ActiQueryResultResVo
     * @Description: 查询活动结果
     */
    public ActiQueryResultResVo queryResult(ActiQueryResultReqVo reqVo) {
        ActiQueryResultResVo resVo = new ActiQueryResultResVo();
        String key = RedisKeyConstant.getActiJoinStatusRedisKey(reqVo.getActId(), reqVo.getUserId());
        String actiJoinStatusStr = redisUtil.get(key);
        if (!StringUtils.hasLength(actiJoinStatusStr)) {
            resVo.setActiJoinStatus(ActiJoinStatEnum.NOT_JOIN.getValue());
        } else {
            resVo.setActiJoinStatus(Integer.parseInt(actiJoinStatusStr));
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/6/3
     * @Params: [reqVo]
     * @Return: ActiQueryPostResVo
     * @Description: 查询活动发货信息
     */
    public ActiQueryPostResVo queryPost(ActiQueryPostReqVo reqVo) {
        ActiQueryPostResVo resVo = new ActiQueryPostResVo();
        List<TActiPost> postList = actiPostService.list();
        if (!CollectionUtils.isEmpty(postList)) {
            List<ActiQueryPostResDTO> resDTOList = new ArrayList<>();
            Map<String, List<TActiPost>> postMap = postList.stream()
                .collect(Collectors.groupingBy(TActiPost::getActiName, LinkedHashMap::new, Collectors.toList()));
            for (String actiName : postMap.keySet()) {
                ActiQueryPostResDTO resDTO = new ActiQueryPostResDTO();
                List<TActiPost> posts = postMap.get(actiName);
                resDTO.setActiName(actiName);
                resDTO.setSaleDay(posts.get(0).getSaleDay());
                resDTO.setPostList(posts);
                resDTOList.add(resDTO);
            }
            resVo.setActiList(resDTOList);
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/6/5
     * @Params: [reqVo]
     * @Return: ActiQueryUserImgResVo
     * @Description: 查询活动用户返图
     */
    public ActiQueryUserImgResVo queryUserImg(ActiQueryUserImgReqVo reqVo) {
        ActiQueryUserImgResVo resVo = new ActiQueryUserImgResVo();
        LambdaQueryWrapper<TActiUserImg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(reqVo.getActiId()), TActiUserImg::getActiId, reqVo.getActiId());
        queryWrapper.orderByAsc(TActiUserImg::getActiId, TActiUserImg::getCardId);
        List<TActiUserImg> userImgList = actiUserImgService.list(queryWrapper);
        resVo.setUserImgList(userImgList);
        return resVo;
    }

}
